package com.hogae.capital.service.impl;

import com.hogae.capital.domain.*;
import com.hogae.capital.enums.AccountTypeEnum;
import com.hogae.capital.enums.AmountTypeEnum;
import com.hogae.capital.mapper.CapUserWalletMapper;
import com.hogae.capital.mapper.CapUserWalletRecordMapper;
import com.hogae.capital.mapper.CapUserWithdrawalAccountMapper;
import com.hogae.capital.mapper.CapWithdrawalRecordMapper;
import com.hogae.capital.service.IWithdrawalService;
import com.hogae.core.core.domain.model.LoginUser;
import com.hogae.core.utils.DateUtils;
import com.hogae.core.utils.StringUtils;
import com.hogae.wukongxinxiang.payment.PaymentUtils;
import com.hogae.wukongxinxiang.payment.request.BankCardRequest;
import com.hogae.wukongxinxiang.payment.response.BankCardResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Objects;

@Service
@Transactional(rollbackFor=Exception.class)
public class WithdrawalServiceImpl implements IWithdrawalService {

    @Autowired
    CapUserWithdrawalAccountMapper accountMapper;

    @Autowired
    CapWithdrawalRecordMapper withdrawalRecordMapper;

    @Autowired
    CapUserWalletMapper userWalletMapper;

    @Autowired
    CapUserWalletRecordMapper userWalletRecordMapper;

    DecimalFormat decimalFormat =new DecimalFormat("#.00");

    @Override
    public String bankCard(Withdrawal withdrawal , LoginUser loginUser) {
        //提现类型：分润或佣金
        if(!Arrays.stream(AmountTypeEnum.values()).anyMatch(a -> a.getCode().equals(withdrawal.getAmountType())))return "请输入正确的提现类型！";
        if(StringUtils.isEmpty(decimalFormat.format(withdrawal.getAmount())) || withdrawal.getAmount().compareTo(BigDecimal.ZERO) <= 0)return "请输入正确的提现金额！";
        CapUserWithdrawalAccount accountInfo = accountMapper.selectAccountInfo(loginUser.getUser().getUserId(), AccountTypeEnum.BANKCARD.getCode());
        if(null == accountInfo)return "未查询到提现信息，请先绑定银行卡后再试！";

        CapUserWallet userWallet = userWalletMapper.selectCapUserWalletByUserId(loginUser.getUser().getUserId());
        if(Objects.isNull(userWallet))return "无用户钱包信息，请查看余额是否充足！";

        //当前用户钱包资金类型可提现额度
        BigDecimal amount = BigDecimal.ZERO;

        if(AmountTypeEnum.COMMISSIONS.getCode().equals(withdrawal.getAmountType())){//佣金
            amount = userWallet.getCommissionsAmount();
        }else if(AmountTypeEnum.PROFIT.getCode().equals(withdrawal.getAmountType())){//分润
            amount = userWallet.getProfitAmount();
        }else {
            return "请输入正确的提现类型！";
        }
        if(amount.compareTo(withdrawal.getAmount()) <= 0)return "提现金额大于可提现金额！";
        //发送提现请求，调用第三方接口转账
        BankCardRequest request = new BankCardRequest();
        //银行卡号
        request.setBankCardNo(accountInfo.getAccountNumber());
        //金额
        request.setAmount(decimalFormat.format(withdrawal.getAmount()));
        //身份证号
        request.setIdCardNo(accountInfo.getIdcard());
        //名字
        request.setName(accountInfo.getName());
        //手机号
        request.setMobileNo(accountInfo.getPhoneNumber());
        BankCardResponse response = PaymentUtils.send(request);

        //插入提现记录
        insertCapWithdrawalRecord(withdrawal,response,loginUser);
        //插入钱包变动记录
        insertCapUserWalletRecord(withdrawal,loginUser);

        return response.getDescription();
    }
    //插入钱包变动记录
    void insertCapUserWalletRecord(Withdrawal withdrawal,LoginUser loginUser){
        CapUserWalletRecord walletRecord = new CapUserWalletRecord();
        walletRecord.setAmount(withdrawal.getAmount());
        walletRecord.setAmountType(withdrawal.getAmountType());
        walletRecord.setUserId(loginUser.getUser().getUserId());
        walletRecord.setCreateTime(DateUtils.getNowDate());
        userWalletRecordMapper.insertCapUserWalletRecord(walletRecord);
    }
    //插入提现记录
    void insertCapWithdrawalRecord(Withdrawal withdrawal ,BankCardResponse response,LoginUser loginUser){
        CapWithdrawalRecord withdrawalRecord = new CapWithdrawalRecord();
        withdrawalRecord.setAmount(withdrawal.getAmount());
        withdrawalRecord.setWithdrawalType(AccountTypeEnum.BANKCARD.getCode());
        withdrawalRecord.setWithdrawalStatus(response.getCode());
        withdrawalRecord.setWithdrawalMessage(response.getDescription());
        withdrawalRecord.setAmountType(withdrawal.getAmountType());
        withdrawalRecord.setCommissionAmount(BigDecimal.ZERO);
        withdrawalRecord.setRealityAmount(withdrawal.getAmount());
        withdrawalRecord.setServiceAmount(BigDecimal.ZERO);
        withdrawalRecord.setUserId(loginUser.getUser().getUserId());
        withdrawalRecord.setCreateTime(DateUtils.getNowDate());
        withdrawalRecordMapper.insertCapWithdrawalRecord(withdrawalRecord);
    }
}
